<?php

class Default_RegistroController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    	$this->_mapper = new Usuarios_Model_UsuarioMapper();
    	$this->view->messages = $this->_helper->flashMessenger->getMessages();
    	
    }

    public function indexAction()
    {
        // action body
        $form = new Usuarios_Form_Registro();
        $this->view->form = $form;
        $this->view->headTitle()->append('Registro');
        if ($this->getRequest()->isPost()) {
        	$data = $this->getRequest()->getPost();
        	if ($form->isValid($data)) {
        		$dbAdapter = Zend_Db_Table::getDefaultAdapter();
        		$adapter = new Zend_Auth_Adapter_DbTable($dbAdapter, "usuarios",
        				"usuario", "senha", 'SHA1(?)');
        		$usuario = new Usuarios_Model_Usuario($form->getValues());
        		$this->_mapper->saveUser($usuario);
        		$this->_helper->flashMessenger->addMessage('Novo usuario adicionado com sucesso!');
        		$this->_helper->redirector("index");
        	} else {
        		$form->populate($data);
        	}
        }
        
    }

    

}



